package com.report.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/9 13:39
 */
public class CustomDecimalSerialize extends JsonSerializer<BigDecimal> {

    @Override
    public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (bigDecimal != null) {
            bigDecimal = bigDecimal.setScale(2, RoundingMode.HALF_UP);
            jsonGenerator.writeString(bigDecimal.toString());
        }

    }
}
